#############################################################################
####State Capacity, Insurgency, and Civil War -Baseline Models Robustness####
#############################################################################
library(MASS) 
library(pscl)
library(foreign)
library(Hmisc)
library(rgdal)
library(RArcInfo)
library(stargazer)
library(mvtnorm)
library(Zelig)
library(readstata13)
library(pROC)
library(robust)
library(cvTools)
library(boot)
library(maps)
library(mapdata)
library(doBy)
library(raster)
library(survival)
library(spduration)
library(DAMisc)
library(plyr)
library(DataCombine)
library(plm)
library(AER)
library(lmtest)

########################
###Create The Dataset###
########################
###Importing the main data
#set working directory to location of dta file
setwd("~/Google Drive/NSLC/Final Dofiles/")
#Import the data
main.data <- read.dta("ntlgrid.dta")

##########################
###CW Onset replication###
##########################
####Baseline
#Model 1
log.FL.1.o.b <- glm(onset ~ lnNL_sum + lagcivconflagtemp +  mnt1 + logbdist1 +  
                    logcellarea + logttime + cluster(gid) + factor(ccode) + 
                    year93 + year94 + year95 + year96 + year97 +
                    year98 + year99 + year00 + year01 + year02 + year03 + year04 +year05 +
                    year06 + year07 + year08,
                  data=main.data, family = "binomial")
summary(log.FL.1.o.b)
AIC(log.FL.1.o.b)
#Model 2
#subset the required data for ethnic war
main.data.eth <- main.data[ which(main.data$second > 0.04999),]
log.FL.2.o.b <- glm(onset ~ lnNL_sum + lagcivconflagtemp +  mnt1 + logbdist1 +  
                    logcellarea + logttime + cluster(gid) + factor(ccode) + 
                    year93 + year94 + year95 + year96 + year97 +
                    year98 + year99 + year00 + year01 + year02 + year03 + year04 +year05 +
                    year06 + year07 + year08,
                  data=main.data.eth, family = "binomial")
summary(log.FL.2.o.b)
AIC(log.FL.2.o.b)


####Add GCP
#Model 1
log.FL.1.o.g <- glm(onset ~ lnNL_sum + lagcivconflagtemp + loglagppp +  mnt1 + logbdist1 +  
                      logcellarea + logttime + cluster(gid) + factor(ccode) + 
                      year93 + year94 + year95 + year96 + year97 +
                      year98 + year99 + year00 + year01 + year02 + year03 + year04 +year05 +
                      year06 + year07 + year08,
                    data=main.data, family = "binomial")
summary(log.FL.1.o.g)
AIC(log.FL.1.o.g)
#Model 2
log.FL.2.o.g <- glm(onset ~ lnNL_sum + lagcivconflagtemp + loglagppp + mnt1 + logbdist1 +  
                      logcellarea + logttime + cluster(gid) + factor(ccode) + 
                      year93 + year94 + year95 + year96 + year97 +
                      year98 + year99 + year00 + year01 + year02 + year03 + year04 +year05 +
                      year06 + year07 + year08,
                    data=main.data.eth, family = "binomial")
summary(log.FL.2.o.g)
AIC(log.FL.2.o.g)

####Add Population
#Model 1
log.FL.1.o.p <- glm(onset ~ lnNL_sum + lagcivconflagtemp + loglagppp + loglagpop + mnt1 + logbdist1 +  
                      logcellarea + logttime + cluster(gid) + factor(ccode) + 
                      year93 + year94 + year95 + year96 + year97 +
                      year98 + year99 + year00 + year01 + year02 + year03 + year04 +year05 +
                      year06 + year07 + year08,
                    data=main.data, family = "binomial")
summary(log.FL.1.o.p)
AIC(log.FL.1.o.p)
#Model 2
log.FL.2.o.p <- glm(onset ~ lnNL_sum + lagcivconflagtemp + loglagppp + loglagpop + mnt1 + logbdist1 +  
                      logcellarea + logttime + cluster(gid) + factor(ccode) + 
                      year93 + year94 + year95 + year96 + year97 +
                      year98 + year99 + year00 + year01 + year02 + year03 + year04 +year05 +
                      year06 + year07 + year08,
                    data=main.data.eth, family = "binomial")
summary(log.FL.2.o.p)
AIC(log.FL.2.o.p)


#Export to latex
stargazer(log.FL.1.o.b, log.FL.2.o.b, log.FL.1.o.g, log.FL.2.o.g, log.FL.1.o.p, log.FL.2.o.p)
